using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO;
using System.Windows.Forms;
using EnterpriseDT.Net.Ftp;
using System.Security.Cryptography;

namespace PicturePage
{
    public partial class FTPSettingsForm : Form
    {
        private Configuration config;

        public FTPSettingsForm(ref Configuration configuration)
        {
            InitializeComponent();

            config = configuration;
            try
            {
                this.mtxtPassword.Text = config.GetDecryptedPassword();
                this.txtServerAddress.Text = config.GetDecryptedServerAddress();
                this.txtUsername.Text = config.GetDecryptedUserName();
                this.numPort.Value = config.GetDecryptedServerPort();
            }
            catch { }
        }

        /// <summary>
        /// Cancels any changes
        /// </summary>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Saves the settings
        /// </summary>
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (chkUsePassword.Checked)
            {
                PopupBox popupBox = new PopupBox();
                popupBox.StartPosition = FormStartPosition.CenterScreen;
                config.ConfigPassword = popupBox.ShowInputBox("Input Password", "Please Input your Password.");
            }
            else
            {
                MessageBox.Show("Are you sure you want to save without a password?\r\nYou will not be able to view these ", "Are you sure?", MessageBoxButtons.OKCancel);
            }

            config.SetEncryptedServerAddress(txtServerAddress.Text);
            config.SetEncryptedServerPort(numPort.Value.ToString());
            config.SetEncryptedUserName(txtUsername.Text);
            config.SetEncryptedPassword(mtxtPassword.Text);

            Configuration.Serialize("config.xml", config);

            this.DialogResult = DialogResult.OK;
        }
    }
}